<template>
    <el-form ref="baseForm" :model="form" :rules="rules" label-width="80px">
        <el-row>
            <el-col :span="12">
            <el-form-item label="代码标识" prop="codeType">
                <el-input v-model="form.codeType"></el-input>
            </el-form-item>
            </el-col><el-col :span="12">
            <el-form-item label="名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="模型类型" prop="seqType">
                <dx-select v-model="form.seqType" placeholder="请选择" codeType="CODE_MODEL_TYPE" @change="seqTypeChange"></dx-select> 
            </el-form-item>
            </el-col><el-col :span="12">
            <el-form-item label="分类" prop="categories">
                <dx-select v-model="form.categories" placeholder="请选择" codeType="CODE_MODEL_CATEGROIES"></dx-select> 
            </el-form-item>
            </el-col>
        </el-row>
        <el-row v-if="form.seqType=='dict'">
            <el-col :span="24">
                <el-form-item label="字典引用" prop="referenceCode" :rules="{ required: true, message: '请选择字典引用', trigger: 'change' }">
                    
                    <el-select v-model="form.referenceCode">
                        <el-option v-for="(item,index) in referenceCodeList" :label="item.text" :value="item.value" :key="index"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row v-else-if="form.seqType=='seq_pojo'||form.seqType=='tree_pojo'">
            <el-col :span="24">
                <el-form-item label="Pojo引用" prop="referenceCode" :rules="{ required: true, message: '请选择Pojo引用', trigger: 'change' }">
                    
                    <el-select v-model="form.referenceCode">
                        <el-option v-for="(item,index) in referenceCodeList" :label="item.text" :value="item.value" :key="index"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row v-else>
            <el-col :span="24">
                <el-form-item label="SQL" prop="sqlText" :rules="{ required: true, message: '请输入SQL文本', trigger: 'blur' }">
                    <el-input type="textarea" :rows="6" v-model="form.sqlText"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="显示字段" prop="textField">
                <el-input v-model="form.textField"></el-input>
            </el-form-item>
            </el-col><el-col :span="12">
            <el-form-item label="值字段" prop="valueField">
                <el-input v-model="form.valueField"></el-input>
            </el-form-item>
            </el-col>
        </el-row>
        <el-row v-if="form.seqType=='tree'">
            <el-col :span="12">
            <el-form-item label="父字段" prop="parentField" :rules="{required: true, message: '请输入父字段', trigger: 'blur'}">
                <el-input v-model="form.parentField"></el-input>
            </el-form-item>
            </el-col><el-col :span="12">
            <el-form-item label="父节点值" prop="parentValue" :rules="{required: true, message: '请输入父节点值', trigger: 'blur'}">
                <el-input v-model="form.parentValue"></el-input>
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
            <el-form-item label="排序" prop="orderList">
                <el-input v-model="form.orderList"></el-input>
            </el-form-item>
            </el-col><el-col :span="12">
            <el-form-item label="缓存时间" prop="cachePageTime">
                <el-input v-model="form.cachePageTime"></el-input>
            </el-form-item>
            </el-col>
        </el-row>
        <el-form-item label="启用" prop="enable">
            <el-switch v-model="form.enable"></el-switch>
        </el-form-item>
        <div style="width:220px;margin:20px auto;">
            <el-button type="primary" :loading="isLoading" @click="doSubmit"><span v-if="!isEdit">立即创建</span><span v-else>保存</span></el-button>
            <el-button @click="closeDialog">取消</el-button>
            <el-button v-if="in_array(form.seqType,['tree','seq'])" type="primary" :loading="isCompileLoading" @click="compileRun">编译验证</el-button>
        </div>
    </el-form>
</template>
<script>
import DxSelect from '../../Common/ui/dx-select'
import DxCheckBox from '../../Common/ui/dx-checkbox'
import DxRadio from '../../Common/ui/dx-radio'
import DxText from '../../Common/ui/dx-text'
import http from '../../../assets/js/http.js'
import toolkit from '../../../assets/js/toolkit.js'
import {codeProvider} from '../../../assets/js/provider.js'
export default {

    components : {
        DxSelect,
        DxCheckBox,
        DxRadio,
        DxText
    },
    props :{
        id : String,
    },
    data(){
        return {
            dialogVisible : false,
            isloading : false,
            isCompileLoading : false,
            isEdit : false,
            referenceCodeType : '',
            referenceCodeList : [],
            form : {
                id : this.id,
                codeType : '',
                name : '',
                seqType : 'seq',
                categories : '1',
                sqlText : '',
                referenceCode : '',
                textField : '',
                valueField : '',
                parentField:'',
                parentValue:'',
                orderList : 10,
                cachePageTime : 30000,
                enable : true,
                isSystem : 0,
            },
            rules: {
                codeType :   [{ required: true, message: '请输入代码模型标识', trigger: 'blur' },
                              {pattern: /^[A-Za-z0-9_]+$/, message: '请输入英文字母', trigger: 'blur'}],
                name :       [{ required: true, message: '请输入代码模型名称', trigger: 'blur' }],
                seqType:     [{ required: true, message: '请选择代码类型', trigger: 'change' }],
                categories:  [{ required: true, message: '请选择分类', trigger: 'change' }],
                textField :  [{ required: true, message: '请输入显示字段', trigger: 'blur' }],
                valueField : [{ required: true, message: '请输入值字段', trigger: 'blur' }],
            }
        }
    },
    methods : {
        doSubmit(){
             this.$refs.baseForm.validate(vaild=>{

                if(vaild){
                    if(this.isEdit){
                        this.update();   
                    }else{
                        this.save();
                    }
                }
            });
          
        },
        compileRun(){
            this.$refs.baseForm.validate(vaild=>{

                if(vaild){
                    this.isCompileLoading = !this.isCompileLoading
                    let _data = Object.assign({},this.form);
                    _data.enable = this.form.enable?1:0;
                    _data.id = 0;
                    _data.modifyDate = "";
                    _data.createDate = "";
                    this.apiPostForm('code/compileRun',_data).then(resp=>{

                        if(resp.code==200){
                            _g.toastMsg('success', '编译运行成功！')
                        
                        }else{
                            _g.toastMsg('error', resp.msg);
                        }
                        this.isCompileLoading = !this.isCompileLoading
                    });


                }
            });
            
        },
        update(){

            this.isLoading = !this.isLoading
            let _data = Object.assign({},this.form);
            _data.enable = this.form.enable?1:0;
            _data.isSystem = this.form.isSystem?this.form.isSystem:0;
            _data.modifyDate = moment(this.form.modifyDate).format('YYYY-MM-DD HH:mm:ss');
            _data.createDate = moment(this.form.createDate).format('YYYY-MM-DD HH:mm:ss');

            this.apiPostForm('code/updateSave',_data).then(resp=>{
                this.isLoading = !this.isLoading
                if(resp.code==200){
                    _g.toastMsg('success', '保存成功')
                    this.closeDialog();
                    this.$emit('submitData','update'); 
                }else{
                    _g.toastMsg('error',resp.msg);
                }
                
            });
        },

        save(){
            this.isLoading = !this.isLoading
            let _data = Object.assign({},this.form);
            _data.enable = this.form.enable?1:0;
            _data.id = 0;
            this.apiPostForm('code/addSave',_data).then(resp=>{

                this.isLoading = !this.isLoading
                if(resp.code==200){
                    _g.toastMsg('success', '保存成功')
                    this.closeDialog();
                    this.$emit('submitData','update'); 
                }else{
                    _g.toastMsg('error',resp.msg);
                }
                
            });
        },
        seqTypeChange(val){
            this.form.referenceCode = '';
            this.form.sqlText = '';
            val = val.value;
            if(val=='dict'){
                this.referenceCodeType = 'sys_dict_codelist';
                this.form.textField = 'dictName';
                this.form.valueField = 'dictValue';
                this.form.cachePageTime = 0;
                    
                codeProvider.getList(this.referenceCodeType).then(result=>{
                    this.referenceCodeList = result;
                });
                
            }else if(val=='seq_pojo' || val=='tree_pojo'){
                this.referenceCodeType = 'sys_pojo_codelist';
                this.form.textField = '';
                this.form.valueField = '';
                this.form.cachePageTime = 30000;
                codeProvider.getList(this.referenceCodeType).then(result=>{

                    this.referenceCodeList = result;
                });
            }
            
        },
        closeDialog(){
            this.$emit('update:dialogVisible', false);
        },
        initData(){
            if(this.form.id){
                this.isEdit = true;
                _g.openGlobalLoading();
                this.apiGet('code/getEntry?id='+this.form.id).then(resp=>{
                    _g.closeGlobalLoading();
                    if(resp.code == 200){
                        this.form = resp.data;
                        this.form.enable = resp.data.enable==1?true:false;

                        if(this.form.seqType=='dict'){
                            this.referenceCodeType = 'sys_dict_codelist';
                            codeProvider.getList(this.referenceCodeType).then(result=>{
                                this.referenceCodeList = result;
                            });
                        }else if(this.form.seqType=='seq_pojo'){
                            this.referenceCodeType = 'sys_pojo_codelist';
                            codeProvider.getList(this.referenceCodeType).then(result=>{
                                this.referenceCodeList = result;
                            });
                        }
                        
                    }else{
                        _g.toastMsg("warn",resp.msg);
                        setTimeout(()=>{
                            this.dialogVisible = false;
                        },1000);
                    }
                   
                });
            }else{
                this.isEdit = false;
                this.seqTypeChange(this.form.seqType);
            }
        }
    },
    created(){
        this.initData();
    },
    mounted(){
        this.routeName = this.$route.name;
        _g.closeGlobalLoading();
    },
    mixins:[http,toolkit]
}
</script>

<style scoped>
@import '../../../assets/css/framework.css'
</style>
